<?php

namespace app\index\controller;

use app\model\Category;
use app\model\Nav;
use app\model\Post;
use app\model\User;
use think\facade\Session;
use think\facade\View;
use think\helper\Str;

class Common extends BaseController
{

  public $modelUser = null;

  public function initialize()
  {
    parent::initialize();

    $user_id = Session::get('user_id');

    if (!empty($user_id)) {
      $this->modelUser = User::find($user_id);
    }

    $list_category = Category::getListIndexCategory(2);
    $list_nav = Nav::getListIndexNav(1);
    View::assign('list_category', $list_category);
    View::assign('list_nav', $list_nav);
    View::assign('user', $this->modelUser);
  }

  public function verifyEmail()
  {
    if (empty($this->modelUser)) {
      return false;
    }

    if (get_system_config('user_email_must') != 'must') {
      return true;
    }

    if ($this->modelUser->is_email_verify == 1) {
      return true;
    }

    return $this->error('请认证邮箱', url('User/set'));
  }


  public function forceLogin()
  {
    if (empty($this->modelUser)) {
      return $this->error('请前往登录', url('Login/index'));
    }
  }
}
